//
//  CZStatusPictureView.swift
//  Blog
//
//  Created by whitcast on 16/7/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


//  重用标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"
//  每项的间距
private let itemMargin: CGFloat = 5
//  每项的宽度
private let itemWidth: CGFloat = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3


// 配图视图

class CZStatusPictureView: UICollectionView {
    // 配图数据源
    var picUrls: [CZStatusPictureInfo]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcPictureViewSize(picUrls?.count ?? 0)
            // 设置配图指定大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            // 重新加载数据
            self.reloadData()
        }
    }
    // MARK: -- 懒加载
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.yellowColor(), fontSize: 30)
        return label
    }()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: flowLayout)
        // 设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        // 水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        // 垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        
        setUpUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    private func setUpUI() {
        // 注册cell
        registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        // 设置数据源代理
//        dataSource = self
        
        backgroundColor = UIColor.orangeColor()
        
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    private func calcPictureViewSize(count: Int) -> CGSize {

        
        //  列数
        let cols = count > 3 ? 3 : count
        
        //  行数
        let rows = (count - 1) / 3 + 1
        //  计算当前配图的一个宽度
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //  计算当前配图的一个高度
        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        let size = CGSize(width: width, height: height)
        
        return size

    }
}

// MARK: -- UICollectionViewDataSource

extension CZStatusPictureView: UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! CZStatusPictureViewCell
        cell.picInfo = picUrls![indexPath.item]
        cell.backgroundColor = UIColor.redColor()
        return cell
    }
}


// 自定义配图cell
class CZStatusPictureViewCell:UICollectionViewCell{
    // 设置模型
    var picInfo: CZStatusPictureInfo? {
        didSet {
            if let imageUrl = picInfo?.thumbnail_pic{
                imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                gifImageView.hidden = !imageUrl.hasPrefix(".gif")
            }
        }
    }
    
    // MARK: -- 懒加载
    // 配图的图片
    private lazy var imageView: UIImageView = {
       let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //  保证原始图片比例显示，图片会超出
        view.contentMode = UIViewContentMode.ScaleAspectFill
        // 剪掉多余部分
        view.clipsToBounds = true
        
        return view
    }()
    // gif 图片
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 添加控件设置约束
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }

}